//
//  JZHDovConViewController.m
//  极致荟
//
//  Created by hetao on 14-5-13.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHDovConViewController.h"
#import "JZHShare.h"
#import "JZHChatViewController.h"
#import "JZHMyNameViewController.h"
#import "ImageCache.h"
@interface JZHDovConViewController ()
@property (weak, nonatomic) IBOutlet UIButton *but3;
@property (weak, nonatomic) IBOutlet UIButton *but2;
@property (weak, nonatomic) IBOutlet UIButton *but1;
@property (weak, nonatomic) IBOutlet UIView *buttview;
@property (weak, nonatomic) IBOutlet UIButton *pop;
@property (strong, nonatomic) IBOutlet UIView *se;
@end

@implementation JZHDovConViewController
int imagemax;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (JZHDovConViewController *)initid:(int)sid
{
    JZHDovConViewController *jzh=[[JZHDovConViewController alloc]init];
    jzh.sid=sid;
    return jzh;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    self.buttview.hidden=NO;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    self.buttview.hidden=YES;
}
- (void)viewDidLoad
{
   
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self loadhttpdata];
    [self.scroll setDelegate:self];
    [self.buttview setFrame:CGRectMake(0, HEIGHT-20, 320, 40)];
    [[UIApplication sharedApplication].windows[0] addSubview:self.buttview];
    [self.but1.layer setCornerRadius:6.0 ];
    [self.but1.layer setMasksToBounds:YES];
    [self.but2.layer setCornerRadius:6.0 ];
    [self.but2.layer setMasksToBounds:YES];
    [self.but3.layer setCornerRadius:6.0 ];
    [self.but3.layer setMasksToBounds:YES];
    if(OSVersionIsAtLeastiOS7>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

///请求网络数据
- (void)loadhttpdata
{
    [MMProgressHUD showWithStatus:@"数据加载中..."];
    NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:self.sid ],@"merId", nil];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"theme/presentView") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        if(rerurncode== HTTPReturnCodeSuccess)
        {
            self.datadic=[data objectForKey:@"data"];
            NSLog(@"%@",self.datadic);
            [self addSubScrollView:self.datadic];
            [MMProgressHUD dismiss];
        }else
        {
            [MMProgressHUD dismissWithError:@"数据加载失败！"];
        }
    }];
}
///添加内容
- (void)addSubScrollView:(NSDictionary *)dic
{
    NSArray *arr=[[dic objectForKey:@"viewdata"] objectForKey:@"detail"];
    NSArray *imgarr=[[dic objectForKey:@"viewdata"] objectForKey:@"image"];
    imagemax=imgarr.count;
    [self.scroll setContentSize:CGSizeMake(320*(arr.count+1), 0)];
    [self.page setNumberOfPages:arr.count+1];
    
    int index=0;
        for(int i=0;i<arr.count;i++)
        {
        UIView *addview=[[UIView alloc]initWithFrame:CGRectMake(320*i, 0, 320, HEIGHT-40)];
            [addview setBackgroundColor:[UIColor clearColor]];
               UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(15, 270, 40, 40)];
        UITextView *text=[[UITextView alloc]initWithFrame:CGRectMake(45, 275, 250, HEIGHT/2-30)];
   
        if(imgarr.count==1)
        { UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 320, HEIGHT/2-30)];
            [ImageCache imageUrl:imgarr[index] UiImageview:image];
            [self.view addSubview:image];
            image.tag=301;
        }else
        {
            UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, HEIGHT/2-30)];
             [ImageCache imageUrl:imgarr[index] UiImageview:image];
            [addview addSubview:image];
        }
                
        
            
        [label setFont:[UIFont boldSystemFontOfSize:25]];
        [label setText:[NSString stringWithFormat:@"0%d",i+1]];
        [label setTextColor:[UIColor grayColor]];
            [text setText:arr[index]];
        [text setUserInteractionEnabled:NO];
            
        [addview addSubview:label];
        [addview addSubview:text];
        index++;
        [self.scroll addSubview:addview];
        }
    [self.se setFrame:CGRectMake(320*arr.count, 0, self.se.frame.size.width, self.se.frame.size.height)];
    [self.scroll addSubview:self.se];
    
}

#pragma mark----scorllview delete-----
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{

    UIImageView *imgae=(UIImageView *)[self.view viewWithTag:301];
    CGFloat x=scrollView.contentOffset.x/320;
   
    if(imagemax==1 && scrollView.contentOffset.x>320*self.page.numberOfPages)
    {
        [imgae setHidden:YES];
    }else
    {
        [imgae setHidden:NO];
    }
   
    if(fmod (x,1 )==0)
    {
        [_page setCurrentPage:x];
        
    }
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (IBAction)pop:(id)sender
{
    [self.buttview removeFromSuperview];
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)share:(id)sender
{
    JZHShare *share=[[[NSBundle mainBundle]loadNibNamed:@"JZHShare" owner:self options:nil] objectAtIndex:0];
    [share show:1 viewcontroller:self];
}
- (IBAction)but1:(id)sender {
    [self.navigationController pushViewController:[[JZHChatViewController alloc]init] animated:YES];
}
- (IBAction)but2:(id)sender {
    [self.navigationController pushViewController:[[JZHMyNameViewController alloc]initName:@"提交资料"] animated:YES];
}
- (IBAction)but3:(id)sender {
}
@end
